﻿using System;
using ArtMath.Attribute;
using ArtMath.Core.Data;
using ArtMath.Component.Point;
using ArtMath.Resource;
using System.Collections.Generic;

namespace ArtMath.Component.Circle
{
    /// <summary>
    /// 圆心定点圆
    /// </summary>
    [Serializable]
    public class CenterPointCircle : FullCircleBase
    {
        public CenterPointCircle(SketchPad pad, string name, bool addToList = true) : base(pad, name, ToolTypeName.yxddy, addToList)
        {
            InPoints = new PointBase[2];
        }

        [MultilingualCategory("Structure"), MultilingualDisplayName("CircleCenter")]
        public PointBase CenterPoint
        {
            get { return InPoints[0]; }
            set { SetPoint(0, value); }
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("PointOnCircle")]
        public PointBase PointOnCircle
        {
            get { return InPoints[1]; }
            set { SetPoint(1, value); }
        }
        public override PointD NewCenter
        {
            get { return CenterPoint.NewXY; }
        }
        public override void SetPoint(int index, PointBase value)
        {
            base.SetPoint(index, value);
            if (value != null)
            {
                switch (index)
                {
                    case 0:
                        ske.Center = value.ToPointD();
                        pske.Center = value.Pxy;
                        break;
                }
            }
        }
        public override void ComponentBuilding(Base component)
        {
            if (OrderIndex == 0)
            {
                if (CenterPoint == null) return;
                Radius = CenterPoint.ToPointD().DistanceTo(Pad.PxyToXY(SpareCoordinate));
                ShowStatus($"{Resources.Translate("Creating")}{Resources.ColonSeparate}{Name}{Resources.LeftParenthesis}{string.Format(Resources.Translate("Of"), Resources.Translate("PointOnCircle"), Resources.Translate("Coordinate"))}{Resources.ColonSeparate}{Pad.PxyToMath(SpareCoordinate)}");
            }
            else
            {
                if (PointOnCircle == null) return;
                pske.Center = SpareCoordinate_;
                ske.Center = Pad.PxyToXY(SpareCoordinate);
                Radius = PointOnCircle.ToPointD().DistanceTo(ske.Center);
                ShowStatus($"{Resources.Translate("Creating")}{Resources.ColonSeparate}{Name}{Resources.LeftParenthesis}{string.Format(Resources.Translate("Of"), Resources.Translate("PointOnCircle"), Resources.Translate("Coordinate"))}{Resources.ColonSeparate}{Pad.PxyToMath(SpareCoordinate)}");
            }
            base.ComponentBuilding(component);
        }
        public override double NewRadius
        {
            get
            {
                return CenterPoint.NewXY.DistanceTo(PointOnCircle.NewXY);
            }
        }
        public override PointD NewPCenter
        {
            get
            {
                return CenterPoint.NewPxy;
            }
        }
        public override bool CanDrawVirtual
        {
            get
            {
                if (OrderIndex == 0)
                {
                    if (CenterPoint != null)
                    {
                        return true;
                    }
                }
                else
                {
                    if (PointOnCircle != null)
                    {
                        return true;
                    }
                }
                return false;
            }
        }
        protected override void ClickOrder(IList<PointCreatingArgs> args, int clickTimes, Predicate<PointCreatingArgs> act)
        {
            if (act == null) return;
            if (OrderIndex == 1)
            {
                if (!act(args[1])) return;
                if (!act(args[0])) return;
                return;
            }
            base.ClickOrder(args, clickTimes, act);
        }
        public override void CreateComponents()
        {
            if (OrderIndex == 0)
            {
                if (CenterPoint == null)
                {
                    CenterPoint = Pad.CreatePoint(Resources.Translate("CircleCenter"), SpareCoordinate_);
                    Pad.TrackObj = this;
                }
                else
                {
                    PointOnCircle = Pad.CreatePoint(Resources.Translate("PointOnCircle"), SpareCoordinate_);
                    base.CreateComponents();
                }
            }
            else
            {
                if (PointOnCircle == null)
                {
                    PointOnCircle = Pad.CreatePoint(Resources.Translate("PointOnCircle"), SpareCoordinate_);
                    Pad.TrackObj = this;
                }
                else
                {
                    CenterPoint = Pad.CreatePoint(Resources.Translate("CircleCenter"), SpareCoordinate_);
                    base.CreateComponents();
                }
            }
        }
    }
}
